﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Caching;

namespace ASBCC.WebCache
{
    public class WebCacheBase : IWebCache
    {
        protected bool CheckParameters(string key)
        {
            return !string.IsNullOrEmpty(key);
        }

        protected bool CheckParameters(string key, object value)
        {
            return !string.IsNullOrEmpty(key) && value != null;
        }

        protected virtual CacheDependency GetCacheDependency(string key)
        {
            return null;
        }

        protected virtual void OnBeforeAdd(string key)
        {
        }

        public virtual void Add(string key, object value)
        {
            if (!this.CheckParameters(key, value)) return;
            this.OnBeforeAdd(key);
            HttpRuntime.Cache.Insert(
                key,
                value,
                this.GetCacheDependency(key),
                System.Web.Caching.Cache.NoAbsoluteExpiration,
                System.Web.Caching.Cache.NoSlidingExpiration,
                System.Web.Caching.CacheItemPriority.Default,
                null);
        }

        public virtual object Get(string key)
        {
            if (!this.CheckParameters(key)) return null;
            return HttpRuntime.Cache.Get(key);
        }

        public virtual void Remove(string key, bool useDependency)
        {
            if (!this.CheckParameters(key)) return;
            HttpRuntime.Cache.Remove(key);
        }
    }
}
